// This file implement the classes defined in CDPPTabFile.h
#ifndef _LINUX_OS_
  #include "stdafx.h"
#else
  #include "w_stdfile.h"
#endif
#include "DAPTabFile.h"
///////////////////////////////////////////////////////////////////
// Implement CDAPTabField
//
CDAPTabField :: CDAPTabField(PTNO nID, DAPValueType nType)
{
	m_nFieldID=nID;
	m_nValueType=nType;
	memset(&m_uValue, 0, sizeof(DAPValue));
}
CDAPTabField :: ~CDAPTabField()
{
	if(m_nValueType==DAP_MSG_TYPE && m_uValue.sValue!=0)
		free(m_uValue.sValue);
}
void CDAPTabField :: SetValue(CDAPString szValue)
{
	SetStrValue((const char*)szValue,m_uValue,m_nValueType);
}

//////////////////////////////////////////////////////////////////
// Implement CDAPTabRow
//
CDAPTabRow :: CDAPTabRow()
{
}
CDAPTabRow :: ~CDAPTabRow()
{
	RemoveAll();
}
void CDAPTabRow :: RemoveAll()
{
	while(!m_lstRowValue.IsEmpty())
		delete m_lstRowValue.RemoveHead();
}
bool CDAPTabRow :: CreateRow(CDAPTabRow &tabTitle)
{
	RemoveAll();
	POSITION pos=tabTitle.m_lstRowValue.GetHeadPosition();
	while(pos){
		CDAPTabField *pField=tabTitle.m_lstRowValue.GetNext(pos);
		CDAPTabField *pNewField=new CDAPTabField(pField->m_nFieldID, pField->m_nValueType);
		m_lstRowValue.AddTail(pNewField);
	}
	return true;
}
bool CDAPTabRow :: FormValue(CDAPString szValue)
{
	CDAPStringArray arrContents;
	ParseStringEx(szValue, arrContents,',');
	for(int i=0; i<arrContents.GetSize()-1;i++){
		POSITION pos=m_lstRowValue.FindIndex(i);
		if(pos!=0){
			CDAPTabField *pField=m_lstRowValue.GetAt(pos);
			pField->SetValue(arrContents[i+1]);
		}
	}
	return true;
}
/////////////////////////////////////////////////////////////////
// Implement 
CDAPTabFile :: CDAPTabFile()
{

}
CDAPTabFile :: ~CDAPTabFile()
{
	while(!IsEmpty())
		delete RemoveHead();
}
bool CDAPTabFile :: LoadFromFile(CDAPString szPathName)
{
	while(!IsEmpty())
		delete RemoveHead();
	m_tabTitle.RemoveAll();
	CStdioFile fileTab;
	CDAPString szTemp;
#ifndef _LINUX_OS_
	CString szMyTemp;
#else
	CDAPString szMyTemp;		//20030820
#endif
#ifndef _LINUX_OS_
	if(!fileTab.Open(szPathName, CFile::typeText | CFile::modeRead))
#else
    if(!fileTab.Open((const char*)szPathName, typeText | modeRead))
#endif
		return false;
	// Load the Title
	while(fileTab.ReadString(szMyTemp)){
		szTemp=szMyTemp;
		szTemp.TrimLeft();
		if(szTemp.GetLength()>2 && !szTemp.Left(2).CompareNoCase("//"))//Comments
			continue;
		CDAPStringArray arrItem;
		ParseStringEx(szTemp, arrItem,',');
		if(arrItem.GetSize()<=0){
			fileTab.Close();
			return false;
		}
		for(int i=0; i<arrItem.GetSize();i++){
			DAPValueType nType=(DAPValueType)atoi((const char*)arrItem[i]);
			CDAPTabField *pField=new CDAPTabField(i,nType);
			m_tabTitle.m_lstRowValue.AddTail(pField);
		}
		break;
	}
	// Load Row
	while(fileTab.ReadString(szMyTemp)){
		szTemp=szMyTemp;
		szTemp.TrimLeft();
		if(szTemp.GetLength()>2 && !szTemp.Left(2).CompareNoCase("//"))//Comments
			continue;
		// Create new row
		CDAPTabRow *pNewRow=new CDAPTabRow();
		pNewRow->CreateRow(m_tabTitle);
		if(pNewRow->FormValue(szTemp))
			AddTail(pNewRow);
		else
			delete pNewRow;
	}
	return true;
}
